package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.OperatorData;

import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class OperatorImpl implements OperatorDao {
    @Override
    public List<OperatorData> findAll() {
        return DbHelp.getQueryAll(OperatorData.class);
    }

    @Override
    public List<OperatorData> findByLoginAndPwd(String login, String pwd) {
        QueryBuilder qb = new QueryBuilder(OperatorData.class).whereEquals(OperatorData.LOGIN, login)
                .whereAnd(OperatorData.PASSWORD, new String[]{pwd});
        return DbHelp.liteOrm.query(qb);
    }

    @Override
    public List<OperatorData> findByLoginAndPwd(String login) {
        QueryBuilder qb = new QueryBuilder(OperatorData.class).whereEquals(OperatorData.LOGIN, login);
        return DbHelp.liteOrm.query(qb);
    }

    @Override
    public OperatorData findByOperid(int id) {
        QueryBuilder qb = new QueryBuilder(OperatorData.class).whereEquals(OperatorData.OPEROATR_ID, id);
        List<OperatorData> operatorDataList = DbHelp.liteOrm.query(qb);
        if (null == operatorDataList || operatorDataList.size() ==0 ) return null;
        return operatorDataList.get(0);
    }

    @Override
    public void addAll(List<OperatorData> operatorData) {
        DbHelp.insertAll(operatorData);
    }

    @Override
    public void addOperator(OperatorData operatorData) {
        DbHelp.insert(operatorData);
    }

    @Override
    public void updateAll(List<OperatorData> operatorData) {
        DbHelp.updateALL(operatorData);
    }

    @Override
    public void updateOperator(OperatorData operatorData) {
        DbHelp.update(operatorData);
    }

    @Override
    public void deleteAll() {
        DbHelp.deleteAll(OperatorData.class);
    }

    @Override
    public void deleteOperator(OperatorData operatorData) {
        DbHelp.delete(operatorData);
    }

    @Override
    public void deleteByOperId(int operId) {
        DbHelp.deleteWhere(OperatorData.class, OperatorData.OPEROATR_ID,
                new String[]{String.valueOf(operId)});
    }
}

